package com.sx.pipeline.ui.task.frag

import android.os.Bundle
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.utils.Logs
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ReleaseTaskListAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseFragment
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.app.event.RefreshEvent
import com.sx.pipeline.app.event.TypeEvent
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.databinding.FragReleaseTaskBinding
import com.sx.pipeline.repos.http.res.MessageListVo
import com.sx.pipeline.repos.http.res.ReleaseTaskVo
import com.sx.pipeline.ui.task.ReleaseTaskVM
import io.reactivex.rxjava3.schedulers.Schedulers


/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description:
 */
class ReleaseTaskFragment :
    CYDataBindingBaseFragment<FragReleaseTaskBinding, ReleaseTaskVM>(R.layout.frag_release_task),View.OnClickListener {
    private lateinit var releaseTaskAdapter: ReleaseTaskListAdapter
    private var status = 0 //任务状态

    companion object {
        fun newInstance(position: Int) =
            ReleaseTaskFragment().apply {
                arguments = Bundle().apply {
                    putInt(IntentKey.ID, position)
                }
            }
    }


    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        status = arguments?.getInt(IntentKey.ID)!!
        mViewModel.putTaskList(status)
        RxBus.getDefault().toObservable(RefreshEvent::class.java)
            .subscribeOn(Schedulers.io())
            .subscribe {
                mViewModel.putTaskList(status)
            }
    }


    private fun initRv() {
        if (!this::releaseTaskAdapter.isInitialized) {
            releaseTaskAdapter = ReleaseTaskListAdapter()
            mBinding.recyclerView.layoutManager =
                LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        }
        releaseTaskAdapter.setOnItemClickListener { adapter, view, position ->
            var info = adapter.data[position] as ReleaseTaskVo
            RouteManager.Mine.intentToReleaseTaskDetail(info.id)
        }
        mBinding.recyclerView.adapter = releaseTaskAdapter

        //取消发布
        releaseTaskAdapter.cancelPostCallback = {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asConfirm(
                    getString(R.string.hint), getString(R.string.txt_cancel_post_task_tips),
                    getString(R.string.cancel), getString(R.string.confirm),
                    {
                        //取消任务
                        mViewModel.getCancelTask(it)
                    }, null, false
                ).show()
        }
        //结束任务
        releaseTaskAdapter.endTaskCallback = {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asConfirm(
                    getString(R.string.hint), getString(R.string.txt_end_task_tips),
                    getString(R.string.cancel), getString(R.string.confirm),
                    {
                        //结束任务
                        mViewModel.getEndTask(it)
                    }, null, false
                ).show()
        }

        //删除任务
        releaseTaskAdapter.deleteTaskCallback = {
            XPopup.Builder(context)
                .isDestroyOnDismiss(true)
                .asConfirm(
                    getString(R.string.hint), getString(R.string.txt_delete_task_tips),
                    getString(R.string.cancel), getString(R.string.confirm),
                    {
                        //删除任务
                        mViewModel.getRemoveTask(it)
                    }, null, false
                ).show()
        }
    }


    override fun initViewObservable() {
        super.initViewObservable()

        mViewModel.releaseTaskListEvent.observe(this) {
            initRv()

            val taskList = it as List<ReleaseTaskVo>
            if (mViewModel.page == 1) {
                if (taskList.isNotEmpty()) {
                    releaseTaskAdapter?.data = taskList as MutableList<ReleaseTaskVo>
                    mBinding.recyclerView.visibility = View.VISIBLE
                    mBinding.emptyView.visibility = View.GONE
                } else {
                    mBinding.recyclerView.visibility = View.GONE
                    mBinding.emptyView.visibility = View.VISIBLE
                }
                mBinding.refreshLayout.finishRefresh()
            } else {
                if (taskList.isNotEmpty()) {
                    releaseTaskAdapter?.addData(taskList)
                    mBinding.refreshLayout.finishLoadMore()
                } else {
                    mBinding.refreshLayout.finishLoadMoreWithNoMoreData()
                }
            }
        }

        observe(mViewModel.successTaskEvent) {
            if (it == true) {
                mViewModel.page = 1
                mViewModel.putTaskList(status)
            }
        }

    }

    override fun initListener() {
        super.initListener()
        mBinding?.refreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page += 1
                mViewModel.putTaskList(status)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page = 1
                mViewModel.putTaskList(status)
            }
        })


        mBinding.tvAdd.setOnClickListener {
            RouteManager.Mine.intentToTaskReleaseAdd()
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            //跳转发布任务
            R.id.tv_add -> {
                RouteManager.Mine.intentToTaskReleaseAdd()
            }
        }
    }

}